package com.example.springboot.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Audit;
import com.example.springboot.entity.Choose;
import com.example.springboot.entity.Review;
import com.example.springboot.entity.User;
import com.example.springboot.service.ChooseService;
import com.example.springboot.service.ReviewService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/teacher")
public class ReviewController {
    @Autowired
    ReviewService reviewService;
    @Autowired
    UserService userService;
    @Resource
    ChooseService chooseService;

    //    新增用户信息
    @PostMapping("/add")
    public Result add(@RequestBody Review review){
        try {
            reviewService.save(review);
        }catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据异常");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
    //修改用户信息
    @PutMapping("/update")
    public Result update(@RequestBody Review review){
        reviewService.updateById(review);
        return Result.success();
    }
    //删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        reviewService.removeById(id);
        return Result.success();
    }
    //查询全部用户信息
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Review> reviewList = reviewService.list(new QueryWrapper<Review>().orderByDesc("id"));  //select * from user order by id desc
        return Result.success(reviewList);
    }

    //根据ID查询用户信息
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Review review= reviewService.getById(id);
        return Result.success(review);
    }

    //多条件分页模糊查询用户信息

    @GetMapping("/selectByPage")
    public Result selectByPage( @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam String name,
                                @RequestParam String projectName
    ){
        QueryWrapper<Review> queryWrapper = new QueryWrapper<Review>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(name), "name" ,name);
        queryWrapper.like(StrUtil.isNotBlank(projectName), "project_name" ,projectName);
        // select *from user where username like '%#{username}%' and name like '%#{name}%'
        User user =TokenUtils.getCurrentUser();
        if (ObjectUtil.isNull(user)){
            return Result.error("请登录");
        }
        if ("教师".equals(user.getRole())){
            List<String> studentIds = chooseService.getStudentIds(user.getId());
            List<Integer> studentIdsInt = new ArrayList<>();
            for (String studentId : studentIds) {
                studentIdsInt.add(Integer.parseInt(studentId));
            }

            queryWrapper.in("user_id", studentIdsInt);
        }
        Page<Review> page = reviewService.page(new Page<>(pageNum, pageSize), queryWrapper);
        reviewService.getReviewWithAudit(page,queryWrapper);
        return Result.success(page);
    }


}